﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Krilib.Data;

namespace Krilib.Control
{
    public abstract class CommandButton : Base.TimelineMouseControllableItem
    {
        public string Description { get; set; }

        public Image Image { get; set; }

        public bool Enabled { get; set; }

        public bool Checked { get; set; }

        public abstract void OnClick();

        public void Draw(Graphics g)
        {
            CommandButtonRenderer.Draw(
                g,
                new Rectangle(ItemPosition, new Size(CommandButtonRenderer.ButtonWHSize, CommandButtonRenderer.ButtonWHSize)),
                this);

        }

        public Point ItemPosition { get; set; }
        public override IEnumerable<Rectangle> ItemRect
        {
            get
            {
                yield return new Rectangle(ItemPosition, new Size(CommandButtonRenderer.ButtonWHSize, CommandButtonRenderer.ButtonWHSize));
            }
        }
    }

    /// <summary>
    /// Command button rendering class
    /// </summary>
    public static class CommandButtonRenderer
    {
        //Button outer size
        public const int ButtonWHSize = 18;
        //Button inner size
        public const int ButtonInnerSize = 16;

        /// <summary>
        /// Draw command button
        /// </summary>
        /// <param name="g">Graphics obj</param>
        /// <param name="rect">Destination rect</param>
        /// <param name="cmdBtn">Draw cmdbtn object</param>
        public static void Draw(Graphics g, Rectangle rect, CommandButton cmdBtn)
        {
            if (cmdBtn.Checked)
            {
                using (var b = new SolidBrush(Color.FromArgb(180, Color.SkyBlue)))
                    g.FillRectangle(b, rect);
                g.DrawRectangle(Pens.RoyalBlue, rect);
            }

            //resizing rect for inner draw
            int gap = ButtonWHSize - ButtonInnerSize;
            rect.Width -= gap;
            rect.Height -= gap;
            rect.X += gap / 2;
            rect.Y += gap / 2;

            if(cmdBtn.Image.Width > ButtonInnerSize || cmdBtn.Image.Height > ButtonInnerSize)
            {
                //scaling draw
                g.DrawImage(cmdBtn.Image, rect);
            }
            else
            {
                Point drawStartPoint = new Point(0, 0);
                drawStartPoint.X += (ButtonInnerSize - cmdBtn.Image.Width) / 2;
                drawStartPoint.Y += (ButtonInnerSize - cmdBtn.Image.Height) / 2;
                //unscaled
                g.DrawImageUnscaled(cmdBtn.Image, drawStartPoint);
            }
        }
    }
}
